\section{Code for problem 2}
\label{code2}
\small
\begin{verbatim}
# "Ruin boundary"-function
boundary <- function(x, scale)
{
  return(-scale*(1+(x/scale)^2))
}

# We will simulated up to 100 X_i's
k<-1:100

# The parameters of this problem
u<-100
meanX <- 1
varX <- 2

###############################################################
# Visualization of the problem at hand.
###############################################################

# Plot the boundary of the ruinous region
plot(k, boundary(k, u), type='l', ylim=c(-max(k)*2,max(k)*2),
	ylab="")

# Mark the point 100*x_0
point<-c((sqrt(13)-1)/6, -1-(sqrt(13)-1)^2/36)
points(point[1]*100, point[2]*100, pch=4, lwd=2)

# Add 100 simulated sample paths under the true measure
for(i in 1:100)
{
  lines(k, cumsum(rnorm(length(k),meanX,sqrt(varX))), type='l',
  	ylim=c(-max(k)*2,max(k)*2))
}

###############################################################
# Visualization of the shifted measure.
###############################################################

# Plot the boundary of the ruinous region
plot(k, boundary(k, u), type='l', ylim=c(-max(k)*2,max(k)*2),
	ylab="")

# The estimated \alpha_0
alpha02 <- -((sqrt(13)-1)^2/6+sqrt(13)+5)/(varX*(sqrt(13)-1))
alpha01 <- -alpha02*(1+alpha02)

# Mean and variance of the shifted measure
shiftedMeanX <- 1+varX*alpha02
shiftedVarX <- varX

# Add 100 simulated sample paths under the shifted measure
for(i in 1:100)
{
  lines(k, cumsum(rnorm(length(k),shiftedMeanX,sqrt(shiftedVarX))),
  	type='l', ylim=c(-max(k)*2,max(k)*2))
}

##############################################################
# Estimate E_nu[\alpha(Z)] by simulating N = 1000 paths of 
# under the shifted measure.
##############################################################

# Number of repetitions
ruinprob <- function(M)
{
    N<-1000
    res <- c()
    # Vector for collecting the simualted z's
    for (j in 1:M)
    {
        z <- c()
        for(i in 1:N)
        {
          # We draw a sample path under the shifted measure
          sample <- cumsum(rnorm(length(k),shiftedMeanX,sqrt(shiftedVarX)))
          
          # We find the time of ruin
          Tu <- 0
          for (i in 1:length(k))
          {
            if (sample[i] < boundary(i,u*M))
            {
                Tu <- i
                break
            }
          }

          # If we haven't observed ruin yet we set z = 0
          if (Tu == 0)
            z <- c(z, 0)
          # Else we calculate z
          else
            z <- c(z, exp(Tu*(alpha01+alpha02+alpha02^2*varX/2))*
                exp(-alpha01*Tu-alpha02*sample[Tu]))
                # kappa^Tu*e^(-alpha*S_Tu)
        }

        # Estimate the probability and confidence for the estimate
        browser()
        tres <- c()
        tres$ruinProb <- mean(z)
        tres$stdRuinProb <- sqrt(var(z))
        tres$confidence <- 1.96 * tres$stdRuinProb / sqrt(N)
        tres$u = u * M

        res <- c(tres, res)
    }
    
    return(res)
}
debug(ruinprob)
u100 <- ruinprob(15)

###############################################################
# Estimate the probability by crude Monte Carlo.
###############################################################

# Using bruteforce Monte Carlo
default <- function (T, N, sigma_2, defaultLevel)
{
  stdDev <- sqrt(sigma_2)
  n_default <- rep(0,N)
  for (i in 1:N)
  {
    sample <- cumsum(rnorm(T,1,sqrt(sigma_2)))
  
    Tu <- 0
    for (i in 1:length(sample))
    {
        if (sample[i] < boundary(i,100))
        {
            n_default[i] <- 1
            break
        }
    }
  }
  
  res <- c()
  res$p_default <- mean(n_default)
  res$var <- var(n_default)
  
  return (res)
}

T <- 100
N <- 100000
sigma_2 <- 2
defaultLevel <- -100

estimate <- default(T, N, sigma_2, defaultLevel)
estimate$p_default
estimate$var
\end{verbatim}
\newpage